<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程编号" prop="classNumber">
        <el-input
          v-model="queryParams.classNumber"
          placeholder="请输入课程编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科任老师" prop="teacherId">
        <el-input
          v-model="queryParams.teacherId"
          placeholder="请输入科任老师"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程状态" prop="courseStatus">
        <el-select v-model="queryParams.courseStatus" placeholder="请选择课程状态" clearable>
          <el-option
            v-for="dict in course_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课程分类" prop="courseTypeId">
        <el-select v-model="queryParams.courseTypeId" placeholder="请选择课程分类" clearable>
          <el-option
            v-for="dict in course_typelist"
            :key="dict.id"
            :label="dict.classTypeName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:course:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:course:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:course:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:course:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-row :gutter="20" v-loading="loading">
      <el-col v-for="course in courseList" :key="course.id" :span="8">
        <router-link :to="'/course_details/index/' + course.id" class="link-type">
        <el-card>
           <el-image
              style="width: 100%; height: 200px"
              :src="course.courseUrl"
              :preview-src-list="[course.courseUrl]"
            />
          <div class="course-info">
            <h3>{{ course.courseName }}</h3>
          </div>
        </el-card>
      </router-link>
      </el-col>
    </el-row>

    <!-- <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center" prop="id" width="100px" />
      <el-table-column label="课程编号" align="center" prop="classNumber" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="课程分类" align="center" prop="courseTypeId">
        <template #default="scope">
          <template v-for="item in course_typelist" :key="item.id">
            <span v-if="item.id === scope.row.courseTypeId">{{ item.classTypeName }}</span>
          </template>
        </template>
      </el-table-column>

      <el-table-column label="科任老师" align="center" prop="teacherId" />
      <el-table-column label="课程状态" align="center" prop="courseStatus">
        <template #default="scope">
          <dict-tag :options="course_type" :value="scope.row.courseStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="课程的章节数" align="center" prop="numOfChapters" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:course:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:course:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table> -->
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程展示对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="courseRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程编号" prop="classNumber">
          <el-input v-model="form.classNumber" placeholder="请输入课程编号" />
        </el-form-item>
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="科任老师" prop="teacherId">
          <el-input v-model="form.teacherId" placeholder="请输入科任老师" />
        </el-form-item>
        <el-form-item label="课程状态" prop="courseStatus">
          <el-select v-model="form.courseStatus" placeholder="请选择课程状态">
            <el-option
              v-for="dict in course_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="课程分类" prop="courseTypeId">
          <el-select v-model="form.courseTypeId" placeholder="请选择课程分类">
            <el-option
              v-for="dict in course_typelist"
              :key="dict.id"
              :label="dict.classTypeName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        


        <!-- <el-form-item label="课程的章节数" prop="numOfChapters">
          <el-input v-model="form.numOfChapters" placeholder="请输入课程的章节数" />
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Course">
// import { useRouter } from '../../../router/index';
import { listCourse, getCourse, delCourse, addCourse, updateCourse } from "@/api/manage/learn";
import {listCourseType} from "@/api/manage/courseType";
import { useRouter } from 'vue-router';

// 获取 router 实例
const router = useRouter();
const { proxy } = getCurrentInstance();
const { course_type } = proxy.useDict('course_type');
// const { queryParams, form, rules } = toRefs(data);
const courseList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    classNumber: null,
    courseName: null,
    courseTypeId: null,
    teacherId: null,
    courseStatus: null,
  },
  rules: {
    classNumber: [
      { required: true, message: "课程编号不能为空", trigger: "blur" }
    ],
    courseName: [
      { required: true, message: "课程名称不能为空", trigger: "blur" }
    ],
    courseTypeId: [
      { required: true, message: "课程分类不能为空", trigger: "change" }
    ],
    teacherId: [
      { required: true, message: "科任老师不能为空", trigger: "blur" }
    ],
  }
});



const { queryParams, form, rules } = toRefs(data);

/** 查询课程展示列表 */
function getList() {
  loading.value = true;
  listCourse(queryParams.value).then(response => {
    courseList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    classNumber: null,
    courseName: null,
    courseTypeId: null,
    teacherId: null,
    courseStatus: null,
    creatTime: null,
    updataTime: null,
    isDelete: null,
    numOfChapters: null
  };
  proxy.resetForm("courseRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加课程展示";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getCourse(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改课程展示";
  });
}

// 跳转函数
// 跳转函数
function handleselect(row) {
  this.reset();
  const id = row.id;
  // getCoin(id).then(response => {
  //   this.$router.push('/course_details/index/'+id)
  // });
  this.$router.push({
          path:'/course_details/index/'+id,
          // query:{
          //   id:id
          // }
        })

  
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["courseRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCourse(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCourse(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除课程展示编号为"' + _ids + '"的数据项？').then(function() {
    return delCourse(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/course/export', {
    ...queryParams.value
  }, `course_${new Date().getTime()}.xlsx`)
}
/**获取课程分类列表 */
const course_typelist = ref([]);
function getCourseTypeList() {
  listCourseType(queryParams.value).then(response => {
    course_typelist.value = response.rows;
    console.log(course_typelist.value);
  });
}




getCourseTypeList();
getList();
</script>
